<template>
  <el-container class="home-containe">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>后台管理系统</span>
      </div>
      <!-- 退出按钮 -->
      <el-button type="info" @click='logout'>退出</el-button>
    </el-header>
    <el-container>
      <!--  左侧导航栏 ，通过是否折叠来控制宽-> -->
      <el-aside :width="isCollapse? '64px' :'200px'">
        <div class="toggleCollapse" @click='toggleCollapse'>|||</div>
        <!-- 一级导航:collapse控制折叠，collapse-transition折叠动画:router="true"开启路由 default-active当前激活菜单的index -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" :unique-opened="true"
          :collapse="isCollapse" :collapse-transition="false" :router="true" :default-active="activePath">
          <el-submenu :index="item.id +''" v-for="item in menuList" :key="item.id">
            <!-- 一级模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文字 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"
              @click='saveNavState("/"+subItem.path)'>
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文字 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>
      <!-- 右侧主体区 -->
      <el-main>
        <!-- 添加子组件的占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    data() {
      return {
        //接收左侧菜单数据
        menuList: [],
        // icon图标,
        iconObj: {
          // 索引号：字体图标，渲染到页面
          '125': 'iconfont icon-user',
          '103': 'iconfont icon-tijikongjian',
          '101': 'iconfont icon-shangpin',
          '102': 'iconfont icon-danju',
          '145': 'iconfont icon-baobiao'
        },
        // 默认不折叠
        isCollapse: false,
        // 点击导航状态记录
        activePath: ""
      };
    },
    created() {
      // 组件私有数据,在创建阶段就进行请求获取数据
      this.getMenuList();
      // 一渲染的时候就将activePath的状态赋值，使其高亮
      this.activePath = window.sessionStorage.getItem("activePath")
    },
    mounted() {

    },
    methods: {
      //退出按钮： 退出主页
      logout() {
        //清空token值
        window.sessionStorage.clear()
        //跳转到login页面
        this.$router.push("/login")
      },
      // 获取所有菜单的请求数据
      async getMenuList() {
        const {
          data: res
        } = await this.$http.get("menus")
        // 判断状态码，获取失败
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        // 获取成功，给左侧菜单数据赋值
        this.menuList = res.data
        console.log(res)
      },
      //   点击折叠
      toggleCollapse() {
        this.isCollapse = !this.isCollapse //取反
      },
      //   激活点击导航高亮
      saveNavState(activePath) {
        // console.log(activePath);
        // 将点击的index保存到sessionStorage中
        window.sessionStorage.setItem("activePath", activePath)
        this.activePath = activePath //赋值高亮
      }
    }
  };

</script>

<style scoped lang="less">
  .home-containe {
    height: 100%;
  }

  .el-header {
    background-color: #373D41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;

    >div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background-color: #333744;

    .el-menu {
      border-right: none;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }

  .iconfont {
    margin-right: 10px;
  }

  // 折叠
  .toggleCollapse {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
  }

</style>
